<template>
<div class="container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
      <h3 class="title">管理员登录</h3>
      <el-form-item prop="username">
        <el-input
            v-model="loginForm.username"
            type="text"
            auto-complete="off"
            placeholder="账号"
        >
          <i slot="prefix" class="el-icon-user" />
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
            v-model="loginForm.password"
            type="password"
            auto-complete="off"
            placeholder="密码"
            @keyup.enter.native="handleLogin"
        >
          <i slot="prefix" class="el-icon-lock" />
        </el-input>
      </el-form-item>
      <el-button size="medium" type="warning" style="width:100%;" @click.native.prevent="handleLogin">登录</el-button>
    </el-form>
</div>
</template>

<script>
import {recordLog} from "@/api/recordLog";

export default {
  name: "AdminLogin",
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, message: '账号不能为空', trigger: 'blur' }],
        password: [{ required: true, message: '密码不能为空', trigger: 'blur' }]
      },
    }
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          if (this.loginForm.username === 'admin' && this.loginForm.password === 'admin') {
            sessionStorage.setItem('admin','admin')
            let routeUrl = this.$router.resolve({
              path:"/adminhome"
            })
            window.open(routeUrl.href, '_blank')
            recordLog({ username: this.loginForm.username, operate: '管理员登录管理系统' })
          }else{
            this.$message({
              message: '账号或密码错误',
              type: 'warning',
              center: true
            })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style scoped lang="less">
.container {
  height: calc(100vh - 100px);
  background-color: #fcf3ec;
  display: -webkit-flex;
  display: flex;
  justify-content: center;
  align-items: center;
  .login-form {
    border-radius: 6px;
    background: #fff;
    width: 350px;
    padding: 25px 25px 25px 25px;
    .title {
      margin: 0 auto 30px auto;
      text-align: center;
      color: #707070;
    }
  }
}

.el-button--warning.is-active,
.el-button--warning:active {
  background: #4d4c50;
  border-color: #4d4c50;
  color: #fff;
}

.el-button--warning:focus,
.el-button--warning:hover {
  background: #575559;
  border-color: #575559;
  color: #fff;
}

.el-button--warning {
  color: #FFF;
  background-color: #4d4c50;
  border-color: #4d4c50;
}
</style>
